package com.nolon.flange.model

import androidx.room.ColumnInfo
import androidx.room.Entity

@Entity(tableName = "flange_table")
data class Flange(
    val length: Int,
    val width: Int,
    @ColumnInfo(name = "extra_length") val extraLength: Int = 0,
    @ColumnInfo(name = "extra_width") val extraWidth: Int = 0,
    @ColumnInfo(name = "material_spec") val materialSpec: String = "30",
    @ColumnInfo(name = "actual_material_spec") val actualSpec:  Int = 0,
    @ColumnInfo(name = "has_rounding") val hasRounding: Boolean,
    // X Big Holes
    @ColumnInfo(name = "x_big_hole_count") val xBigHoleCount: Int = 0,
    @ColumnInfo(name = "x_big_hole_padding") val xBigHolePadding: Float = 0f,
    @ColumnInfo(name = "x_big_hole_distance") val xBigHoleDistance: Float = 0f,
    // X Small Holes
    @ColumnInfo(name = "x_small_hole_count") val xSmallHoleCount: Int = 0,
    @ColumnInfo(name = "x_small_hole_padding") val xSmallHolePadding: Float = 0f,
    @ColumnInfo(name = "x_small_hole_Distance") val xSmallHoleDistance: Float = 0f,
    // Y Big Holes
    @ColumnInfo(name = "y_big_hole_count") val yBigHoleCount: Int = 0,
    @ColumnInfo(name = "y_big_hole_padding") val yBigHolePadding: Float = 0f,
    @ColumnInfo(name = "y_big_hole_distance") val yBigHoleDistance: Float = 0f,
    // X Small Holes
    @ColumnInfo(name = "y_small_hole_count") val ySmallHoleCount: Int = 0,
    @ColumnInfo(name = "y_small_hole_padding") val ySmallHolePadding: Float = 0f,
    @ColumnInfo(name = "y_small_hole_distance") val ySmallHoleDistance: Float = 0f
) {
    private fun partX() = Part(
        quantity = length + 2 * actualSpec + extraLength,
        materialSpec = materialSpec,
        actualSpec = actualSpec,
        rounding = hasRounding,
        bigHoles = xBigHoleCount,
        bigPadding = xBigHolePadding,
        bigDistance = xBigHoleDistance,
        smallHoles = xSmallHoleCount,
        smallPadding = xSmallHolePadding,
        smallDistance = xSmallHoleDistance,
        direction = Part.DIRECTION_LENGTH,
    )

    private fun partY() = Part(
        quantity = width + extraWidth,
        materialSpec = materialSpec,
        actualSpec = actualSpec,
        rounding = false,
        bigHoles = yBigHoleCount,
        bigPadding = yBigHolePadding,
        bigDistance = yBigHoleDistance,
        smallHoles = ySmallHoleCount,
        smallPadding = ySmallHolePadding,
        smallDistance = ySmallHoleDistance,
        direction = Part.DIRECTION_WIDTH,
    )

    fun getParted(double: Boolean = false): List<Part> {
        val partOne = partX()
        val partTwo = partY()
        return if (double) listOf(partOne, partOne, partTwo, partTwo) else listOf(partOne, partTwo)
    }

}
